package bll

import (
	"context"
	"fmt"
	"manager/store/redis/base"
)

const (
	OnlineRedisKey = "signal:online"
)

// IsOnline check device is online
func IsOnline(sn string) bool {
	return base.GetClient().ExistsHashItem(context.Background(), OnlineRedisKey, sn)
}

// GetSignalAddressBySn get signal address by sn
func GetSignalAddressBySn(sn string) string {
	return base.GetClient().GetHashItem(context.Background(), OnlineRedisKey, sn)
}

func RpcCall(sn string, callback func(addr string, err error) error) error {
	addr := GetSignalAddressBySn(sn)
	if addr == "" {
		return callback(addr, fmt.Errorf("车辆不在线"))
	}
	return callback(addr, nil)
}
